package com.mvc4g.example.client.views;

import com.mvc4g.example.client.beans.ProductBean;
import com.smartgwt.client.types.HeaderControls;
import com.smartgwt.client.types.Overflow;
import com.smartgwt.client.types.VerticalAlignment;
import com.smartgwt.client.widgets.AnimationCallback;
import com.smartgwt.client.widgets.Button;
import com.smartgwt.client.widgets.Canvas;
import com.smartgwt.client.widgets.Img;
import com.smartgwt.client.widgets.Label;
import com.smartgwt.client.widgets.Window;
import com.smartgwt.client.widgets.events.ClickEvent;
import com.smartgwt.client.widgets.events.ClickHandler;
import com.smartgwt.client.widgets.layout.HStack;
import com.smartgwt.client.widgets.layout.VStack;

public class PopupView {

	private Window popup = null;

	private Label description = getPopupLabel();
	private Label price = getPopupLabel();

	private HStack photos = new HStack();
	private Img photo = new Img(null, 250, 250);

	private ProductBean product = null;

	public PopupView() {

		photos.setAutoHeight();
		
		Canvas photoCanvas = new Canvas();
		photoCanvas.setSize("250", "250");
		photoCanvas.addChild(photo);

		VStack leftPart = new VStack();
		leftPart.setWidth(300);
		leftPart.addMember(photoCanvas);
		leftPart.addMember(photos);

		description.setValign(VerticalAlignment.TOP);
		description.setHeight(200);
		description.setOverflow(Overflow.AUTO);		
		price.setAutoHeight();

		Button add = getPopupButton("Ajouter au panier");
		add.setIcon("/images/popup/panier.gif");
		Button contact = getPopupButton("Contacter le vendeur");		
		contact.setIcon("/images/popup/contact.gif");
				
		
		HStack buttons = new HStack();
		buttons.setAutoHeight();
		buttons.setMargin(10);
		buttons.addMember(add);
		buttons.addMember(contact);

		VStack rightPart = new VStack();
		rightPart.setWidth(310);
		rightPart.addMember(getTitleLabel("Prix"));
		rightPart.addMember(price);
		rightPart.addMember(getTitleLabel("Description"));
		rightPart.addMember(description);
		rightPart.addMember(buttons);
		
		HStack mainPanel = new HStack();
		mainPanel.setPadding(5);
		mainPanel.addMember(leftPart);
		mainPanel.addMember(rightPart);

		popup = new Window();
		popup.setAutoSize(true);
		popup.setIsModal(true);
		popup.setCanDragResize(true);
		popup.addItem(mainPanel);
		popup.setShowMinimizeButton(false);

	}

	public void setProduct(ProductBean product) {
		this.product = product;

		popup.setTitle(product.getTitle());
		description.setContents(product.getDescription());
		price.setContents(product.getPrice());

		photos.removeMembers(photos.getMembers());
		int id = product.getId();
		int nbPhotos = product.getNbPhotos();
		for (int i = 0; i < nbPhotos; i++) {
			photos.addMember(getPopupPhoto("/images/product/" + id + "_" + i + ".jpg"));
		}

		photo.setSrc("/images/product/" + id + "_" + 0 + ".jpg");

	}

	public void draw() {
		popup.draw();
	}

	private Label getTitleLabel(String text) {
		Label title = new Label(text);
		title.setStyleName("popupTitle");
		title.setAutoHeight();		   
		return title;
	}

	private Label getPopupLabel() {
		Label label = new Label();
		label.setStyleName("popupLabel");
		return label;
	}

	private Button getPopupButton(String text) {
		Button button = new Button(text);
		button.setAutoFit(true);		
		return button;
	}

	private Img getPopupPhoto(final String src) {
		Img img = new Img(src, 50, 50);
		img.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(ClickEvent event) {
				photo.animateRect(0, 0, 0, 0, new AnimationCallback() {

					@Override
					public void execute(boolean earlyFinish) {
						photo.setSrc(src);
						photo.animateRect(0, 0, 250, 250,
								new AnimationCallback() {

									@Override
									public void execute(boolean earlyFinish) {
										// TODO Auto-generated method stub

									}

								}, 1000);
					}

				}, 1000);

			}

		});

		return img;
	}

}
